/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.Region;
import org.crystalrange.enterprise.service.RegionService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class RegionListController extends BaseController {

    protected RegionService regionService;
    protected Region selectedRegion;
    protected Listbox listboxRegion;
    protected Search searchRegion;
    protected List<Region> regions;

    public RegionService getRegionService() {
        return this.regionService;
    }

    public void setRegionService(RegionService regionService) {
        this.regionService = regionService;
    }

    public Region getSelectedRegion() {
        return this.selectedRegion;
    }

    public void setSelectedRegion(Region selectedRegion) {
        this.selectedRegion = selectedRegion;
    }

    public List<Region> getRegions() {
        return this.regions;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchRegion = new Search(Region.class);
        this.searchRegion.setMaxResults(100);
        this.searchRegion.addFetch("createdBy");
        this.searchRegion.addFetch("updatedBy");
        this.regions = this.regionService.search(this.searchRegion);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemRegionCreate(Event event) throws Exception {
        this.regionCreate();
    }

    public void onClick$menuitemRegionEdit(Event event) throws Exception {
        this.regionEdit();
    }

    public void onClick$menuitemRegionDelete(Event event) throws Exception {
        this.regionDelete();
    }

    public void onClick$menuitemRegionSearch(Event event) throws Exception {
        this.regionSearch();
    }

    public void onClick$menuitemRegionRefresh(Event event) throws Exception {
        this.regionRefresh();
    }

    public void onClick$toolbarbuttonRegionCreate(Event event) throws Exception {
        this.regionCreate();
    }

    public void onClick$toolbarbuttonRegionEdit(Event event) throws Exception {
        this.regionEdit();
    }

    public void onClick$toolbarbuttonRegionDelete(Event event) throws Exception {
        this.regionDelete();
    }

    public void onClick$toolbarbuttonRegionSearch(Event event) throws Exception {
        this.regionSearch();
    }

    public void onClick$toolbarbuttonRegionRefresh(Event event) throws Exception {
        this.regionRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.regionEdit();
    }
    
    private void regionCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/region.zul");
    }

    private void regionEdit() throws Exception {
        if (this.selectedRegion != null) {
            this.execution.sendRedirect("~./zul/html/region.zul?Id=" + this.selectedRegion.getId().toString());
        }
    }

    private void regionDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxRegion.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Region region = (Region) selectedItem.getValue();
                    this.regionService.remove(region);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.regions = this.regionService.search(this.searchRegion);
        this.binder.loadAll();
    }

    private void regionSearch() throws Exception {
        this.execution.setAttribute("searchRegion", this.searchRegion);

        Window window = (Window) this.execution.createComponents("~./zul/html/regionSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchRegion = (Search) this.execution.getAttribute("searchRegion");
        }

        this.regions = this.regionService.search(this.searchRegion);
        this.binder.loadAll();
    }

    private void regionRefresh() throws Exception {
        this.regions = this.regionService.search(this.searchRegion);
        this.binder.loadAll();
    }
}
